
import 'package:find_friend/common/alert_tool.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/network/network.dart';
import 'package:find_friend/pages/xspace/model/artclemodel.dart' show ReplyModel;
import 'package:find_friend/pages/xspace/model/school_model.dart' show ArticleModel;
import 'package:flutter/material.dart';

import 'raise_animated.dart';


class XpageCommentWidget extends StatefulWidget {

  final ArticleModel model; // 文章赞
  final ReplyModel replyModel; // 评论赞
  final Function() onRefreshData;
  final Function() onTapComment;

  XpageCommentWidget({
    Key key, 
    this.model, 
    this.replyModel,
    this.onRefreshData, 
    this.onTapComment,
  }) : super(key: key);

  @override
  _XpageCommentWidgetState createState() => _XpageCommentWidgetState();
}

class _XpageCommentWidgetState extends State<XpageCommentWidget> with SingleTickerProviderStateMixin {

  Animation<double> _animation;
  Animation<double> _scaleAnimation;
  AnimationController _animationController;
  int upvotes = 0;
  @override
  void initState() {
    super.initState();

    _animationController = AnimationController(vsync: this, duration: const Duration(milliseconds: 200));
    CurvedAnimation _cureve = CurvedAnimation(parent: _animationController, curve: Curves.bounceInOut);
    _animation = Tween<double>(begin: 0, end: 1).animate(_cureve);
    _scaleAnimation = Tween<double>(end: 0.0, begin: 1.0).animate(_cureve);

    // 列表刷新的时候会重新走一遍
    if(_isupvotes()) _animationController.forward(); 
    if(_isdislikeflag()) _animationController.reverse(); 
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  void didUpdateWidget(XpageCommentWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    // 列表刷新的时候会重新走一遍
    if(_isupvotes()) _animationController.forward(); 
    if(_isdislikeflag()) _animationController.reverse(); 
  }

  _callback(XCommentAction action) {
    switch (action) {
      case XCommentAction.like:
        _requestLike(true);
        break;
      case XCommentAction.unlike:
        _requestLike(false);
        break;
      case XCommentAction.comment:
        if(widget.onTapComment != null) widget.onTapComment();
        break;
      case XCommentAction.share:
        AlertTool.showBottomShareDialog(context, articleModel: widget.model, onRefreshData: widget.onRefreshData);
        break;
    }
  }

  

  _requestLike(bool islike){
    if(widget.model == null && widget.replyModel == null) return; 
    Map<String, dynamic> param = {};
    if(widget.model != null) param["aid"] = widget.model.id;
    if(widget.replyModel != null) param["rid"] = widget.replyModel.id;
    Networktool.request(API.upvote, params: param, method: islike ? NETWORKHTTPMethod.POST : NETWORKHTTPMethod.DELETE, success: (data){
      // AlertTool.showToast("点赞成功");
      // 点赞计数
      _getupvotes(islike);

      if(widget.model != null) {
        setState(() {
          widget.model.upvotes += upvotes;
        });
      } else {
        setState(() {
          widget.replyModel.upvotes += upvotes;
        });
      }
      islike ? _animationController.forward() : _animationController.reverse();
      if(widget.onRefreshData != null) widget.onRefreshData();
      
    }
    ,fail: (msg) => AlertTool.showToast(msg)
    );
  }

  String _reports() {
    if(widget.model != null) {
      return "${widget.model?.strReports ?? 0}";
    } else {
      // return "${widget.replyModel?.reports ?? 0}";
    }
  }

  String _replys() {
    if(widget.model != null) {
      return "${widget.model?.strReplys ?? 0}";
    } else {
      return "${widget.replyModel?.strReplys ?? 0}";
    }
  }

  String _upvotes() {
    if(widget.model != null) {
      return "${widget.model?.strUpvotes ?? 0}";
    } else {
      return "${widget.replyModel?.strUpvotes ?? 0}";
    }
  }

  bool _isupvotes() {
    if(widget.model != null) {
      return widget.model.upvoteflag != null;
    } else {
      return widget.replyModel.upvoteflag != null;
    }
  }

  bool _isdislikeflag() {
    if(widget.model != null) {
      return widget.model.dissflag != null;
    } else {
      return widget.replyModel.dissflag != null;
    }
  }

  void _getupvotes(bool islike) {
    if(widget.model != null) {
      if(islike){
        upvotes = widget.model.dissflag != null ? 2 : 1;
        widget.model.upvoteflag = 1;
        widget.model.dissflag = null;
      } else {
        upvotes = widget.model.upvoteflag != null ? -2 : -1;
        widget.model.upvoteflag = null;
        widget.model.dissflag = 1;
      }
    } else {
      if(islike){
        upvotes = widget.replyModel.dissflag != null ? 2 : 1;
        widget.replyModel.upvoteflag = 1;
        widget.replyModel.dissflag = null;
      } else {
        upvotes = widget.replyModel.upvoteflag != null ? -2 : -1;
        widget.replyModel.upvoteflag = null;
        widget.replyModel.dissflag = 1;
      }
    }
  }

  

  @override
  Widget build(BuildContext context) {
    
    final size = MediaQuery.of(context).size;
    final width = (size.width - 25 * 2 - 30 * 4) / 3.0;
    
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 15),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          InkWell(
            onTap: () => _callback(XCommentAction.share),
            // child: Image.asset("assets/zhuanfa.png", width: 21,fit: BoxFit.fill,),
            // child: Icon(IconData(0xe603, fontFamily: "iconfont"), size: 21,),
            child: Icon(const IconData(0xe61a, fontFamily: "iconfont"), size: 30,),
          ),
          Container(
            width: width,
            height: 30,
            alignment: Alignment.center,
            child: Text("${widget.model?.reports ?? 0}", style: instanceTextStyle(14, APP_font_black),),
          ),
          InkWell(
            onTap: () => _callback(XCommentAction.comment),
            // child: Image.asset("assets/pinglun.png", width: 21,fit: BoxFit.fill,),
            child: Icon(const IconData(0xe601, fontFamily: "iconfont"), size: 30,),
          ),
          Container(
            width: width,
            height: 30,
            alignment: Alignment.center,
            child: Text(_replys(), style: instanceTextStyle(14, APP_font_black),),
          ),
          InkWell(
            onTap: () {
              _callback(XCommentAction.like);
            },
            child: RiseAnimatedWidget(dissAnimation: _scaleAnimation, likeAnimation: _animation,imgW: 30, iszan: true, value: _isupvotes()),
          ),
          Container(
            width: width,
            height: 30,
            alignment: Alignment.center,
            child: Text(_upvotes(), style: instanceTextStyle(14, APP_font_black),),
          ),
          InkWell(
            onTap: () {
              _callback(XCommentAction.unlike);
            },
            child: RiseAnimatedWidget(dissAnimation: _animation, likeAnimation: _scaleAnimation,imgW: 30, iszan: false, value: _isdislikeflag()),
          ),
        ],
      ),
    );
  }
}

enum XCommentAction {
  /// 分享
  share, 
  /// 评论
  comment,
  /// 点赞
  like,
  /// 踩
  unlike
}